package com.dtborad.myblog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dtborad.myblog.common.CommonErrorCode;
import com.dtborad.myblog.common.Result;
import com.dtborad.myblog.entity.Posts;
import com.dtborad.myblog.entity.Records;
import com.dtborad.myblog.service.RecordService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/record")
@Slf4j
@Api(tags = "奖品模块")
public class RecordController {

    @Autowired
    private RecordService recordService;

    //查询中奖列表
    @GetMapping("/list")
    public Result<Map<String, Object>> list(@RequestParam(defaultValue = "1") int pagenum,
                                            @RequestParam(defaultValue = "5") int pagesize,
                                            @RequestParam(defaultValue = "") String content,
                                            @RequestParam(defaultValue = "") Integer usersid){
        Page<Records> currentPage = recordService.getAllRecordsByPageSize(pagenum, pagesize, usersid, content);
        Map<String, Object> response = new HashMap<>();
        response.put("records", currentPage.getRecords());
        response.put("total", currentPage.getTotal());
        return Result.ofSuccess(response);
    }

    //添加中奖信息
    @PostMapping("/add")
    public Result<String> addRecords(@RequestBody Records records) {
        try {
            Records newRecords = new Records();
            //将传递参数的属性拷贝到新对象中
            BeanUtils.copyProperties(records, newRecords);
            //保存新对象
            newRecords.setWinTime(new Date());
            recordService.save(newRecords);
        } catch (Exception e) {
            return Result.ofFail(CommonErrorCode.FAILURE);
        }
        return Result.ofSuccess();
    }

    //发放奖励
    @PostMapping("/update")
    public Result<String> updateStatus(@RequestBody Records records){
        try {
            Records newRecords = new Records();
            //将传递参数的属性拷贝到新对象中
            BeanUtils.copyProperties(records, newRecords);
            newRecords.setStatus(1);
            recordService.updateById(newRecords);
        }catch (Exception e){

        }
        return Result.ofSuccess();
    }
}
